package com.yfree.global

import com.yfree.config.YConfig
import org.springframework.stereotype.Component
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import java.nio.charset.StandardCharsets
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@Component
class YFilter : HandlerInterceptorAdapter() {
    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        request.characterEncoding = StandardCharsets.UTF_8.name()
        response.characterEncoding = StandardCharsets.UTF_8.name()
        if (YConfig.Response.isJsonHeader)
            response.setHeader("Content-Type", "application/json; charset=${StandardCharsets.UTF_8.name()}")
        return YConfig.Request.filterCallBack(request, response, handler)
    }
}
